class Solution {
    public boolean backspaceCompare(String s, String t) {
        char[] sc = s.toCharArray();
        char[] tc = t.toCharArray();

        int countS = 0, countT = 0;
        int i = sc.length - 1, j = tc.length - 1;

        while (true) {
            while (i >= 0) {
                if (sc[i] == 35) countS++;
                else if (countS == 0) break;
                else countS--;
                i--;
            }

            while (j >= 0) {
                if (tc[j] == 35) countT++;
                else if (countT == 0) break;
                else countT--;
                j--;
            }

            if (i < 0 || j < 0) break;
            if (sc[i] != tc[j]) return false;

            i--;
            j--;
        }

        return i < 0 && j < 0;

    }
}